
var member_id = "";

//
// 服务控件点击
//
function onClickCell(e) {
    var operate = {};

    dev = e.getAttribute("dev");
    sev = e.getAttribute("sev");
    operate = "";

    state = e.getAttribute("state");

    var desc = {};
    desc.major = e.getElementsByClassName("major")[0].innerHTML;
    desc.minor = e.getElementsByClassName("minor")[0].innerHTML;

    var family_msg = "";
    if (state === "offline") {
        family_msg = desc.major + "-" + desc.minor + " 已经离线";
        PGshowNotify(family_msg, "err");
        return
    } else if(state === "online") {
        family_msg = desc.major + "-" + desc.minor + " 是状态设备, 不能进行操作";
        PGshowNotify(family_msg, "err");
        return
    }

    if (state == "open") {
        operate = "close";
    } else {
        operate = "open";
    }

    var request = {};
    request.type = "operate";

    if (state == "open") {
        request.data = "close";
    } else {
        request.data = "open";
    }

    var msg = {};
    msg.dev = dev;
    msg.sev = sev;
    msg.request = request;

    var json = JSON.stringify(msg);

    WSSendMessage(json)
}

//
function parsMessage(post) {

    var obj = JSON.parse(post);

    console.log("parse message");

    if ((obj.dev === "") && (obj.sev === "family-message")){
        PGshowNotify(obj.desc, obj.state);
        return
    }

    PGsetSevState(obj.dev, obj.sev, obj.state, obj.desc);
}


// 点击退出
function onClickExit(e) {
    console.log("onClickExit");

    PGsetStateConn("offline");
    PGshowNotify("退出登录成功", "error");
}

// 点击连接
function onClickConnect(e) {
    console.log("onClickConnect");

    PGsetStateConn("online");
    PGshowNotify("成功连接服务器。。。。。。。。。。。");
    PGsetSevState("WSDMP161206001L", "temp-humd", "online", "温:17.2 湿:42%")
}

//
window.onload = function () {
    console.log("onLoad");
    PGrefreshLayout();

    var member = GetCookie("member_id");
    if (member != null) {
        console.log("member id = ", member);
        member_id = member
    }

   var ws_addr = GetCookie("ws_addr");
    //ws_addr = "ws://adai.design:8081";
    if (ws_addr != null) {
        WSsetURL(ws_addr)
    }

    ServerConn();
};